package com.cn.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import com.cn.entity.Orders;



public class OrdersDao {
	

	private static SessionFactory sessionFactory;
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	Session session;

	
	
	public List<Orders> scanAll(){
		session=sessionFactory.openSession();
		String hql="from Orders";
		Query query=session.createQuery(hql);
		List<Orders> list=query.list();
	   
	     return list;
		
		
	}
	public Orders scanOne(int orderid){
		Orders order=null;
		session=sessionFactory.openSession();
		String hql="from Orders where orderid=?";
		Query query=session.createQuery(hql);
		query.setInteger(0, orderid);
		List<Orders> list=query.list();
	   
		if (list.size()>0){order=list.get(0);}
		
	     return order;
		
		
	}

	public void add(Orders order){
		session=sessionFactory.openSession();
		Transaction tr=session.beginTransaction();
		
		session.save(order);
		tr.commit();
		session.close();
		
		
		
	}
	
	public void update(Orders order){
		session=sessionFactory.openSession();
		Transaction tr=session.beginTransaction();
		
		session.update(order);
		tr.commit();
		session.close();
		
		
		
	}
	
	public void delete(Orders order){
		
		Transaction tr=session.beginTransaction();
		
		session.delete(order);
		tr.commit();
		session.close();
		
		
		
	}
	
	public List<Orders>  scanPart(String date){
		
		session=sessionFactory.openSession();
		String hql="from Orders where date=?";
		Query query=session.createQuery(hql);
		query.setString(0, date);
		List<Orders> list=query.list();
	   
		
		
	     return list;
		
		
	}
	
   public List<Orders>  scanPart(String date,String advise){
		
		session=sessionFactory.openSession();
		String hql="from Orders where date=? and advise=?";
		Query query=session.createQuery(hql);
		query.setString(0, date);
		query.setString(1, advise);
		List<Orders> list=query.list();
	   
		
		
	     return list;
		
		
	}
   public List<Orders>  scanPart(int doctorid){
		
		session=sessionFactory.openSession();
		String hql="from Orders where doctorid=?";
		Query query=session.createQuery(hql);
		query.setInteger(0, doctorid);
		List<Orders> list=query.list();
	   			
	    return list;
		
		
	}
	
   public List<Orders>  scanParts(int userid){
		
		session=sessionFactory.openSession();
		String hql="from Orders where userid=?";
		Query query=session.createQuery(hql);
		query.setInteger(0, userid);
		List<Orders> list=query.list();
	   			
	    return list;
		
		
	}
   
}
